package com.tony.servlet;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tony.intergration.AccountQueryServiceClient;
import com.tony.intergration.HelloServiceClient;
import com.tony.personalcore.dao.dataobject.AccountDO;
import com.tony.util.LoggerUtil;

public class LoginServlet extends HttpServlet {

    /** serialVersionUID */
    private static final long         serialVersionUID = -1590621166592189932L;

    private HelloServiceClient        helloServiceClient;

    private AccountQueryServiceClient accountQueryServiceClient;

    private ApplicationContext        applicationContext;

    private static final Logger       logger           = Logger.getLogger("service.log");

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this
            .getServletContext());
        accountQueryServiceClient = (AccountQueryServiceClient) applicationContext
            .getBean("accountQueryServiceClient");
        helloServiceClient = (HelloServiceClient) applicationContext.getBean("helloServiceClient");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
                                                                          IOException {

        String type = req.getParameter("type");

        if (type != null && type.equals("requireRefreshBalance")) {

            refreshBalance(req, resp);
        } else {

            login(req, resp);
        }

    }

    private void refreshBalance(HttpServletRequest req, HttpServletResponse resp)
                                                                                 throws IOException {

        String email = req.getParameter("email");

        LoggerUtil.info(logger, email + "查询余额进来了！");

        AccountDO accountDO = accountQueryServiceClient.queryByEmail(email);
        resp.getWriter().write("" + accountDO.getBalance());
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException,
                                                                       ServletException {
        String email = req.getParameter("email");

        LoggerUtil.info(logger, email + "登录进来了！");

        AccountDO accountDO = accountQueryServiceClient.queryByEmail(email);

        if (accountDO == null) {
            accountDO = new AccountDO();
            accountDO.setEmail(email);
            accountDO.setBalance(new BigDecimal("0"));

            accountQueryServiceClient.insert(accountDO);

            LoggerUtil.info(logger, email + "账号未注册，开始初始化该账号");

        }

        LoggerUtil.info(logger, email + "登录结果，accountDO=" + accountDO);

        req.setAttribute("accountDO", accountDO);
        req.setAttribute("words", helloServiceClient.sayHello("this is testMaven"));

        req.getRequestDispatcher("/main.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                                                                           throws ServletException,
                                                                           IOException {
        doGet(req, resp);
    }

    public void setHelloServiceClient(HelloServiceClient helloServiceClient) {
        this.helloServiceClient = helloServiceClient;
    }

}
